//index.js
const Controllers = require('../../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    statistics: [
      {
        value: 0,
        text: '员工数量',
        unit: '',
        color: '',
      },
      {
        value: 0,
        text: '提成总金额',
        unit: '¥',
        color: ''
      },
      {
        value: 0,
        text: '员工提现总额',
        unit: '¥',
        color: '#F50E0E',
      }
    ],
    lists: []
  },
  onLoad(option) {
  },
  onShow() {
    var that = this;
    const agentIndexData = app.globalData.agentIndexData;
    if (!agentIndexData || Object.keys(agentIndexData).length === 0) {
      that.agentIndex();
    } else {
      that.setData({
        agentIndexData
      })
    }
    that.staffList();
  },
  agentIndex() {
    var that = this;
    Controllers.agentIndex({}).then(res => {
      const { data } = res;
      app.globalData.agentIndexData = data;
      
      that.setData({
        agentIndexData: data,
      })
    }).catch(err => {
      console.log('agentIndex--err==', err);
    })
  },
  staffList() {
    var that = this;
    wx.showLoading();
    Controllers.staffList({}).then(res => {
      console.log('staffList--res==', res);
      const { code, data, desc } = res;
      var totalCommission = 0; 
      var totalWithdrawal = 0; 
      data.forEach(d => {
        totalCommission += d.commission;
        totalWithdrawal += d.withdrawal;
      })
      that.data.statistics[0].value = data.length;
      that.data.statistics[1].value = totalCommission;
      that.data.statistics[2].value = totalWithdrawal;
      that.setData({
        statistics: that.data.statistics,
        lists: data
      })
      wx.hideLoading();
    })
  },
  updateStaffState(e) {
    console.log('updateStaffState--e==', e);
    var that = this;
    const index = e.currentTarget.dataset.index;
    const agentStaffId = e.currentTarget.dataset.id;
    wx.showLoading();
    Controllers.updateStaffState({agentStaffId}).then(res => {
      console.log('updateStaffState--res==', res);
      const { code, data, desc } = res;
      var lists = that.data.lists;
      lists[index] = { ...data };
      that.setData({
        lists
      });
      wx.hideLoading();
    })
  },
  openPage(e) {
    var url = e.currentTarget.dataset.url;
    if (url) {
      app.openPage({ url });
    } else {
      wx.showToast({
        title: '未获取页面路径',
        icon: 'none'
      });
    }
  }
})